"""
__author__ = 'berlinyoung'
@File   :base_page.py
@Date   :2021/9/4 16:04
-*- coding: utf-8 -*-
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    """
    BasePage提供公共方法的封装，即和页面逻辑无关的封装
    比如解决driver初始化的问题
    """
    # 添加baseurl,可以支持测试用例灵活配置起始页
    # basepage 完全和业务逻辑解耦
    _base_url = "https://work.weixin.qq.com/wework_admin/frame#index"
    def __init__(self,base_driver=None):
        if base_driver is None:
            # 通过remote 复用浏览器操作
            # options = Options()
            # options.debugger_address = "127.0.0.1:9222"
            chrome_arg = webdriver.ChromeOptions()
            # 加入调试地址
            # 需要先将环境从你的虚拟环境 venv切换到本机环境
            chrome_arg.debugger_address = "127.0.0.1:9222"
            # 实例化对象
            self.driver = webdriver.Chrome(options=chrome_arg)
            # 添加隐式等待
            self.driver.implicitly_wait(3)
            # 打开首页
            self.driver.find_element(By.CSS_SELECTOR,"#menu_index").click()
            # self.driver.get(self.base_url)
        else:
            # 将self.driver添加一个WebDriver对象注解，解决类型提示的问题
            # 注解本身没有任何的赋值作用，方便IDE操作
            self.driver:WebDriver = base_driver

    #元素定位方法封装
    def find(self,by,locator = None):
        # 如果只传入一个元组参数
        if locator is None:
            return self.driver.find_element(*by)
        # 适配于多种传参情景
        else:
            return self.driver.find_element(by=by,value=locator)